package com.csust.automaticteach.utils.logging.callback;

import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.entity.OperationLogging;
import com.csust.automaticteach.service.OperationLoggingService;
import com.csust.opration.logger.core.callback.LogCallbackInterface;
import com.csust.opration.logger.core.model.OperationLog;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;


/**
 * 日志回调实现  自定义操作日志
 */
@Slf4j
public class LogCallbackImpl implements LogCallbackInterface {
    @Resource
    private OperationLoggingService operationLoggingService;
    @Override
    public void resolveLog(OperationLog operationLog) {
        // 保存日志至数据库
        //1.构造日志对象
        OperationLogging operationLogging = new OperationLogging();
        operationLogging.setType(operationLog.getType());//类型
        operationLogging.setDescription(operationLog.getContent());// 描述
        operationLogging.setSector(operationLog.getMenu());// 模块
        operationLogging.setUserId(Long.valueOf(operationLog.getOperationUser().getUserId())); // 用户id
        operationLogging.setUserRole(operationLog.getOperationUser().getRole());// 用户角色
        boolean saved = operationLoggingService.save(operationLogging);
        ThrowUtils.throwIf(!saved, ErrorCode.OPERATION_ERROR);
    }
}